package tool;

import javafx.scene.control.Cell;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import java.util.ArrayList;
import java.util.List;

/**
 * @author： 杨浩然
 * @date： 2023-02-02 11:45
 */
public class ExcelWorkBook {
    final static int intervalNum=2;//excel间隔行数

    HSSFWorkbook workBook;
    int excelRow=0;//行号标记
    int excelCell=0;//列号标记
    HSSFSheet sheet;//当前sheet
    HSSFRow row;//当前行
    List<HSSFCellStyle> styles=new ArrayList<HSSFCellStyle>();

    public HSSFWorkbook getWorkBook() {
        return workBook;
    }

    public ExcelWorkBook() {
        this.workBook =  new HSSFWorkbook();

        //单元格样式:居中对齐
        HSSFCellStyle cellStyle = workBook.createCellStyle();
//        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont font = workBook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 11);
        cellStyle.setFont(font);
        styles.add(cellStyle);//第0位 正常

        //单元格样式:居中对齐
        HSSFCellStyle cellStyle1 = workBook.createCellStyle();
//        cellStyle1.setAlignment(HorizontalAlignment.CENTER);
        cellStyle1.setAlignment(HorizontalAlignment.LEFT);
        cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont font1 = workBook.createFont();
        font1.setFontName("宋体");
        font1.setFontHeightInPoints((short) 11);
        font1.setColor(IndexedColors.RED.getIndex());
        cellStyle1.setFont(font1);
        styles.add(cellStyle1);//第1位 警告

    }
    //写入数据 按行写
    public void setRowData(List<String> data){
        for (String v:data){
            getNextRowCell().setCellValue(v);
        }
    }
    //写入数据 按行写警告数据
    public void setRowDataErr(List<String> data){
        for (String v:data){
            HSSFCell nextRowCell = getNextRowCell();
            //合并单元格，必须得调用getNextRowCell()才能减
            sheet.addMergedRegion(new CellRangeAddress(excelRow-1,excelRow-1,excelCell-1 ,excelCell+3 ));
            nextRowCell.setCellStyle(styles.get(1));
            nextRowCell.setCellValue(v);
        }
    }

    //写入数据 按列写
    public void setCellData(List<String> data){
        nextRow();
        for (String v:data){
            getNextCell().setCellValue(v);
        }
    }
    //写入数据
    public void setData(List<List<String>> data){
        for (List<String> rowdata:data){
            nextRow();
            for (String celldata:rowdata){
                getNextCell().setCellValue(celldata);
            }
        }
    }
    //返回样式 0=正常 1=警告
    public HSSFCellStyle getstyles(int i){
       return styles.get(i);
    }

    //增加间隔行
    public void addIntervalNum(){
        goRow(excelRow+intervalNum);
    }

    //创建sheet并设置名称
    public HSSFSheet sheetAt(int i,String name) {
        workBook.createSheet(name);
        sheet= workBook.getSheetAt(i);
        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 3000);
        sheet.setColumnWidth(2, 3000);
        sheet.setColumnWidth(3, 4000);
        sheet.setColumnWidth(4, 4000);
        sheet.setColumnWidth(5, 24000);
        sheet.setColumnWidth(6, 10000);
        excelRow=0;
        return sheet;
    }
    public HSSFSheet sheetAt(int i) {
        sheet= workBook.getSheetAt(i);
        excelRow=0;
        return sheet;
    }

    //当前行后移到下一行
    public ExcelWorkBook nextRow() {
        row=sheet.createRow(excelRow);excelRow++;
        excelCell=0;
        return this;
    }
    //跳转到指定行
    public ExcelWorkBook goRow(int i) {
        row=sheet.createRow(i);
        excelRow=i;
        excelCell=0;
        return this;
    }

    //向后一列，并获取
    public HSSFCell getNextCell() {
        HSSFCell cell = row.createCell(excelCell);excelCell++;
        cell.setCellStyle(styles.get(0));
        return cell;
    }
    //获取指定列
    public HSSFCell getCell(int i) {
        HSSFCell cell = row.createCell(i);
        excelCell=i+1;
        cell.setCellStyle(styles.get(0));
        return cell;
    }
    //获取下一行首列
    public HSSFCell getNextRowCell() {
        nextRow();
        return getNextCell();
    }

}
